package com.tang.companyjob.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.tang.common.api.UserFeignClient;
import com.tang.common.entity.companyJob.Company;
import com.tang.companyjob.mapper.CompanyMapper;

import com.tang.companyjob.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CompanyServiceImpl extends ServiceImpl<CompanyMapper, Company> implements CompanyService {
    @Autowired
    private CompanyMapper companyMapper;
    @Override
    public List<Company> list(String keywords, Integer currentPage, Integer pageSize,Integer hasAuth) {
        PageHelper.startPage(currentPage,pageSize);
        if (keywords == null || "".equals(keywords)) {
            QueryWrapper<Company> qw = new QueryWrapper<Company>();
            qw.eq("delete_flag",1);
            qw.eq("has_Auth",hasAuth);
            return companyMapper.selectList(qw);
        }
        QueryWrapper<Company> queryWrapper = new QueryWrapper<Company>();
        queryWrapper.eq("delete_flag",1).eq("has_Auth",hasAuth).like("company_name",keywords).or().like("user_name",keywords);
        List<Company> companys = companyMapper.selectList(queryWrapper);
        return  companys;
    }

    @Override
    public int deleteByCompanyId(int companyId) {
        Company company = companyMapper.selectById(companyId);
        company.setDeleteFlag(0);
        return companyMapper.updateById(company);
    }

    @Override
    public String auditCompany(Integer companyId, String res) {
        Company company = companyMapper.selectById(companyId);
        if("pass".equals(res) && company!=null){
           company.setHasAuth(1);
            int i = companyMapper.updateById(company);
            if(i!=0){
                return "pass";
            }
        }
        if("noPass".equals(res) &&company!=null){
            company.setHasAuth(2);
            int i = companyMapper.updateById(company);
            if(i!=0){
                return "noPass";
            }
        }
        return null;
    }

    @Override
    public Integer getCompanyCount() {
        return  companyMapper.selectCount(null);
    }



    @Override
    public Company register(Company company) {
        int insert = companyMapper.insert(company);
        if(insert==1){
            QueryWrapper query = new QueryWrapper();
            query.eq("company_name",company.getCompanyName());
            Company company1 = companyMapper.selectOne(query);
            return company1;
        }
        return null;
    }

    @Override
    public Company selectByCompanyName(String companyName) {
        QueryWrapper query = new QueryWrapper();
        query.eq("has_auth",1);
        query.eq("company_name",companyName);
        return companyMapper.selectOne(query);
    }

}
